﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data;
using System.Data.SqlClient;

public partial class Register : System.Web.UI.Page
{
    private static string sqlStr = "Integrated Security=SSPI;Data Source=(local);initial catalog=tsmanager";
    SqlConnection con = new SqlConnection(sqlStr);
    SqlDataAdapter Adapter = null;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string str1 = TextBox1.Text.Trim();
        string str2 = TextBox2.Text.Trim();
        string str3 = TextBox3.Text.Trim();
        string str4 = TextBox4.Text.Trim();
        if (con.State == System.Data.ConnectionState.Closed)
        {
            con.Open();
        }

        string jg;
         jg=selectdata();


         int dzid = 0;
         if (jg == "")
         {
             dzid = 1;
         }
         else
         {
             dzid = Convert.ToInt32(jg);
             dzid++;
         }

         string str = "insert into Readers(ReadersID,ReadersNAME,ReadersGENDER,ReadersCLASS,ReadersCOLLEGE) values ('" + dzid + "','" + str1 + "','" + str2 + "','" + str3 + "','" + str4 + "')";
         SqlCommand SC = new SqlCommand(str, con);
         int n = SC.ExecuteNonQuery();

         if (n == 1)
         {

             Response.Write("<script language=javascript>alert('注册成功！')</script>");
             if (Session["glyid"] != null)
             {
                 string str11 = "insert into ReaderManagement(ReadersID,librarianID,RM_RTIME,RM_LTIME) values ('" + dzid + "','" + Session["glyid"].ToString() + "','" + DateTime.Now.ToShortDateString().ToString() + "','" + "" + "')";
                 SqlCommand SC1 = new SqlCommand(str11, con);
                 SC1.ExecuteNonQuery();
             }
             con.Close();

         }

         else
         {
             Response.Write("<script language=javascript>alert('注册失败！')</script>");

         }


    }



    protected string selectdata()
    {
        int ilen=0;
        string[] dataid=null;
        string sqlcmd = "select * from Readers";
        using (SqlConnection con = new SqlConnection(sqlStr))
        {
            con.Open();
            using (SqlDataAdapter adapt = new SqlDataAdapter(sqlcmd, con))
            {
                try
                {
                    string[] datarow = new string[20];
                    dataid = new string[20];
                    DataSet ds = new DataSet();
                    adapt.Fill(ds, "Readers");
                    ilen = ds.Tables["Readers"].Rows.Count;
                    int j = 1;
                    foreach (DataRow row in ds.Tables["Readers"].Rows)
                    {
                        dataid[j] = row["ReadersID"].ToString();
                        j++;
                        if (j > ilen)
                        {
                            break;
                        }
                    }
                    //导航更新
                  
                }
                catch (Exception ex)
                {
                    Response.Write("<script language=javascript>alert('数据刷新失败！')</script>");
                }
            }

        }

        return dataid[ilen];

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("Login.aspx");
    }
}